/*
 * game.cpp
 *
 *  Created on: 8/11/2008
 *      Author: smaxll
 */

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#include <stdio.h>
#include <stdlib.h>

#include <string>
#include <cstdlib>
#include <iostream>



#include "game_config.hpp"
#include "cyconfig.h"
#include "image.hpp"
#include "preferences.hpp"
#include "cursor.hpp"
#include "loadscreen.hpp"
#include "sound.hpp"


namespace {

class game_controller
{
public:
    game_controller(int argc, char** argv);
    ~game_controller();

    bool init_video();
    bool init_background();
    bool init_config();
    bool init_language();

    void reset_game_cfg();
    const bool is_loading();
    bool load_game();
    void set_tutorial();

    void show_help();
    SDL_Surface* get_screen();
    enum RELOAD_GAME_DATA {RELOAD_DATA, NO_RELOAD_DATA};
    void play_game(RELOAD_GAME_DATA reload=RELOAD_DATA);

private:
    const int argc_;
    int nArg;
    const char* const * const argv_;
    SDL_Surface* screen;
};

game_controller::game_controller(int argc, char** argv)
    : argc_(argc), nArg(0), argv_(argv)
{
    bool no_sound = false;

    for(nArg=0; nArg<argc_; nArg++){
        const std::string valArg(argv_[nArg]);

        if(valArg.empty()) {
            continue;
        }

        if(valArg == "-resolution" || valArg == "-r") {

        }
    }

}

game_controller::~game_controller(){

}


bool game_controller::init_video(){
    /* Initialize the screen */
    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
    }

    /* Make sure SDL_QUIT get called when the program exit */
    atexit(SDL_Quit);

    image::set_wm_icon();

    int x_res, y_res;
    x_res = preferences::resolution().first;
    y_res = preferences::resolution().second;

    screen = SDL_SetVideoMode(x_res, y_res, 16, SDL_SWSURFACE);

    if (screen == NULL) {
        fprintf(stderr, "Unable to set video mode to . %s\n", SDL_GetError());
        return false;
    }

    SDL_WM_SetCaption("尘缘在线", NULL);

    return true;
}

/**
 * Initialize main background.
 */
bool game_controller::init_background(){
    SDL_Surface *image;
    SDL_Rect src, dest;

    /* Load background screen */
    image = IMG_Load("images/background/bg_800.jpg");
    if (image == NULL) {
        fprintf(stderr, "Unable to load the background image %s \n", SDL_GetError());
        // Changed from return 1; to exit(EXIT_FAILURE);
        exit(EXIT_FAILURE);
    }

    src.x = 0;
    src.y = 0;
    src.w = image->w;
    src.h = image->h;

    dest.x = 0;
    dest.y = 0;
//    dest.w = image->w;
//    dest.h = image->h;
    dest.w = screen->w;
    dest.h = screen->h;


    /* Draw the image to the screen */
    SDL_BlitSurface(image, &src, screen, &dest);
    // update the screen
    SDL_UpdateRect(screen, 0, 0, 0, 0);
    // Pause for a blit
//    SDL_Delay(3000);


    // Free memory
    SDL_FreeSurface(image);

    return true;
}

void game_controller::play_game(RELOAD_GAME_DATA reload){

}

SDL_Surface* game_controller::get_screen(){
    return screen;
}


}



/**
 * Process commandline arguments
 * The information is shown on console without a display device.
 */
static int play_game(int argc, char** argv){
    int nArg;
    for(nArg=0; nArg<argc; nArg++){
        std::string argVar(argv[nArg]);
        if(argVar.empty()) {
            continue;
        }

        if(argVar == "--help" || argVar == "-h") {
            std::cout << "Usage: " << argv[0]
                      << " [OPTIONS] [DATA-DIRECTORY]\n"
                      << " -f, -fullscreen                  run the game in full screen mode\n"
                      << " -h, -help                        prints the help information\n"
                      << " -r, -resolution XxY              set the screen resolution, e.g. 800x600"
                      << " -v, -version                     prints game's version number and exit\n"
                      ;
            return 0;
        } else if (argVar == "-v" || argVar == "-version") {
            std::cout << " v" << game_config::version << "\n";
            return 0;
        }
    }

    game_controller game(argc, argv);   // Define an instance of game controller class
    const int start_ticks = SDL_GetTicks();

    bool res; // result

    res = game.init_video();
    if(res == false) {
        std::cerr << "could not initialize display\n";
        return 0;
    }

    res = game.init_background();

    cursor::set(cursor::NORMAL);

    loadscreen::global_loadscreen = new loadscreen(game.get_screen(), 0);
//    loadscreen::global_loadscreen->clear_screen(false);

    loadscreen::global_loadscreen->set_progress(5, "Loading game configuration", true);
    game_config::load_config();

    loadscreen::global_loadscreen->set_progress(100, "Loading title screen.");
    delete loadscreen::global_loadscreen;
    loadscreen::global_loadscreen = NULL;

    std::cerr << "time elapsed: " << (SDL_GetTicks() - start_ticks) << " ms\n";

    sound::init();
    sound::play_music_repeatedly(game_config::titleMusic);

    return 0;
}


int main(int argc, char** argv){

    std::cerr << "ChenY Online v" << VERSION << '\n';

    time_t t = time(NULL);
    std::cerr << "Started on: " << ctime(&t) << '\n';

    int result = play_game(argc, argv);


    SDL_Event event;

    while (SDL_WaitEvent(&event) != 0) {
        switch(event.type){
        case SDL_QUIT:
            printf("Quit CY online. Bye!\n");
            exit(EXIT_SUCCESS);
        }
    }

    sound::close();

    return 0;
}
